/*
 * Transaction.java
 */

package nxtgeninventorysystem.Data;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.sql.Date;

/**
 * The Transaction class is meant to hold data from the Transaction
 * entity so that it can be easily passed as a parameter.
 * @author Seraph Softworks
 */
public class Transaction extends Data {

    public static final String BORROW = "Borrow";
    public static final String RETURN = "Return";
    private static final SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private static final SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");

    private int id;
    private String type;
    private Date date;
    private Person person;
    private ArrayList<Item> items;
    private String purpose;

    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(int id) {
        this.id = id;
    }

    /**
     * @return the type
     */
    public String getType() {
        return type;
    }

    /**
     * @param type the type to set
     */
    public void setType(String type) {
        if(type == null) {
            this.type = "";
            return;
        }
        this.type = type.replace("\\'","\'").replace("\\\"","\"");
    }

    /**
     * @return the date
     */
    public Date getDate() {
        return date;
    }

    /**
     * @param date the date to set
     */
    public void setDate(Date date) {
        this.date = date;
    }

    /**
     * @return the person
     */
    public Person getPerson() {
        return person;
    }

    /**
     * @param person the person to set
     */
    public void setPerson(Person person) {
        this.person = person;
    }

    /**
     * @return the items
     */
    public ArrayList<Item> getItems() {
        return items;
    }

    /**
     * @param items the items to set
     */
    public void setItems(ArrayList<Item> items) {
        this.items = items;
    }

    /**
     * @return the purpose
     */
    public String getPurpose() {
        return purpose;
    }

    /**
     * @param purpose the purpose to set
     */
    public void setPurpose(String purpose) {
        if(purpose == null) {
            this.purpose = "";
            return;
        }
        this.purpose = purpose.replace("\\'","\'").replace("\\\"","\"");
    }

    public String getDateTimeString() {
        return datetimeFormat.format(date);
    }

    public String getDateString() {
        return dateFormat.format(date);
    }

    public String getTimeString() {
        return timeFormat.format(date);
    }

    @Override
    public void reset() {

    }

    public ArrayList <String> getTransactionSearchParametersQueryStrings(TransactionSearchOptions options) {
        ArrayList<String> conditions = new ArrayList<String>();
        if (options.considerDate()) {
            conditions.add("transaction.date like '" + getDateString().replace("\'","\\'").replace("\"", "\\\"") + "%'");
        }
        if (options.considerId()) {
            conditions.add("transaction.id = '" + getId() + "'");
        }
        if (options.considerPurpose()) {
            conditions.add("transaction.purpose like '%" + getPurpose().replace("\'","\\'").replace("\"", "\\\"") + "%'");
        }
        if (options.considerType()) {
            conditions.add("transaction.type = '" + getType().replace("\'","\\'").replace("\"", "\\\"") + "'");
        }
        if (options.considerItem() != null) {
            conditions.add("transaction.id = transactionitem.transaction");
            conditions.add("transactionitem.item = item.id");

            ItemSearchOptions o = options.considerItem();
            Item v = getItems().get(0);

            if (o.considerDescription()) {
                conditions.add("item.description like '%" + v.getDescription().replace("\'","\\'").replace("\"", "\\\"") + "%'");
            }
            if (o.considerId()) {
                conditions.add("item.id = " + v.getId());
            }
            if (o.considerAvailable()) {
                if(v.getPerson() == null)
                    conditions.add("item.person is NULL");
                else
                    conditions.add("item.person is not NULL");
            }
            if (o.considerName()) {
                conditions.add("item.name like '%" + v.getName().replace("\'","\\'").replace("\"", "\\\"") + "%'");
            }
            if (o.considerType()) {
                conditions.add("item.type = '" + v.getType().replace("\'","\\'").replace("\"", "\\\"") + "'");
            }
        }
        if (options.considerPerson() != null) {
            conditions.add("transaction.person = person.id");

            PersonSearchOptions o = options.considerPerson();
            Person v = getPerson();

            if (o.considerContactNo()) {
            conditions.add("person.contact_no like '%" + v.getContactNo().replace("\'","\\'").replace("\"", "\\\"") + "%'");
            }
            if (o.considerId()) {
                conditions.add("person.id = '" + v.getId() + "'");
            }
            if (o.considerName()) {
                conditions.add("person.name like '%" + v.getName().replace("\'","\\'").replace("\"", "\\\"") + "%'");
            }
            if (o.considerTeam()) {
                conditions.add("person.team = '" + v.getTeam().replace("\'","\\'").replace("\"", "\\\"") + "'");
            }
        }
        return conditions;
    }

}
